/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.javanile.chesslang;

/**
 *
 * @author totalinnovation
 */
public class Brain extends Thread {

    private static final int STARTUP	= 0;
    private static final int OPEN	= 1;
    private static final int NEXT	= 2;
    private static final int OTHER	= 3;
    private static final int ESCAPE	= 4;
    private static final int FIRST	= 5;
    private static final int TEST	= 6;

    private static final String[] ACTION = {
	"startup","open","next","other","escape","first","test"
    };

    private int caret;
    private int level;
    private int index;

    private Hash hash;

    private Game    game;
    private int[]   bm;
    private int[]   em,er;
    private int	    val;
    private int	    point,point1;
    private int[][] path;

    private EvaluationFunction f;

    private Console console;
    private boolean active;
    private boolean ponder;
    private boolean startup;
    private int	    action;
    private List    lines;

    public Brain() {
    
    }
    public Brain(Console c) {
	console = c;
	game	= new Game(console);
	active	= true;
	ponder	= false;
	startup = false;
	bm	= new int[] {0,0};
	em	= new int[] {0,0};
	hash	= new Hash(10);
	path    = new int[10][2];
    }

    public void lauch() {
	this.start();
    }

    public void quit() {
	active = false;
	ponder = false;
    }

    public void newgame() {
	game = new Game(console);
    }

    public void newgame(String fen) {
	game = new Game(console, fen);
    }

    public void eat(String c) {
	game.eat(c);
    }

    public void ponder() {
	ponder = true;
	startup = true;
	action = STARTUP;
	level = 0;
    }

    public void halt() {
	ponder = false;
    }

    public String bestmove() {
	return Less.move(bm);
    }

    @Override
    public void run() {
	while(active) {
	    while(ponder) {
		game.domoves(path,level);

		path[level] = game.mainmove();
		
		game.unmoves(level);

		level++;
		
		console.put("info pv "+Less.variant(path,level));

		if (level>9) {
		    console.put("bestmove "+Less.move(path[0]));
		    ponder = false;
		}
	    }
	}
    }
    private void infoline() {
	try {
	    Brain.sleep(30);
	    String line="";
	    //for(int i=0;i<level;i++) {
		//line+=Less.move(hash.get(i, path[i]))+" ";
	    //}
	    /*
	    console.put("info action "+
		Less.fixlen(ACTION[action],7)+
		Less.fixlen(level+":"+path[level],5)+
		Less.fixlen(""+val,5)+
		Less.fixlen(line,50)
	    );*/
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
    }

    public void _debug(String tr) {
	if (false) {
	} else if (tr.startsWith("move ")) {
	    game.eat_move(tr.substring(5));
	} else if (tr.equals("moves")) {
	    _debug_moves();
	} else if (tr.equals("board")) {
	    _debug_board();
	}
    }

    private void _debug_moves() {
	//game.setDebug(true);
	List ms = game.moves();
	//game.setDebug(false);
	
	console.put("id        moves     point     loser     ");
	console.put("----------------------------------------");
	int i = 1;
	while(ms.next()) {
	    int p = game.evalmove(ms.move());
	    int l = game.losemove(ms.move());
	    console.put(
		Less.fixlen( ""+i, 10)+
		Less.fixlen( Less.move(ms.move()), 10)+
		Less.fixlen( ""+p, 10)+
		Less.fixlen( ""+l, 10)+
		""
	    );
	    i++;
	}
    }

    private void _debug_board() {
	console.put(Less.board(game.dump())+"\n");
    }

    public void setEvaluationFunction(EvaluationFunction x) {
	f = x;
    }
}
